#/usr/bin/bash
INSTALL_PATH=$1
if [ ! $INSTALL_PATH ];then
	INSTALL_PATH="/usr/local/redis"
fi

#REDIS架包下载路径,不传默认ftp://ftpUser:123456@175.178.190.189/redis/redis-6.2.6/redis-6.2.6.tar.gz
DOWNLOAD_URL=$2
if [ ! $DOWNLOAD_URL ];then
DOWNLOAD_URL="ftp://ftpUser:123456@175.178.190.189/redis/redis-6.2.6/redis-6.2.6.tar.gz"
fi

REDIS_PORT=$3
if [ ! $REDIS_PORT ];then
REDIS_PORT="6379"
fi


REDIS_PASSWORD=$4
if [ ! $REDIS_PASSWORD ];then
REDIS_PASSWORD="123456"
fi


##获取文件名
FILENAME=${DOWNLOAD_URL##*/};

#如果文件夹不存在，创建文件夹
if [ ! -d $INSTALL_PATH ];then
   mkdir $INSTALL_PATH
fi

cd $INSTALL_PATH

#wget下载文件
wget $DOWNLOAD_URL

#判断当前文件是什么压缩包
if [ ${FILENAME##*.} = gz ];then
  TEMP=${FILENAME%.*}
    if [ ${TEMP##*.} = tar ];then
        tar -zxvf $FILENAME -C $INSTALL_PATH
    else
        unzip  $FILENAME -d  $INSTALL_PATH
    fi
elif [ ${FILENAME##*.} = zip ];then
    unzip  $FILENAME -d  $INSTALL_PATH
fi


#删除安装包
rm -rf $INSTALL_PATH/$FILENAME

#检查GCC是否安装
gcc -v 

#获取解压后当前目录下的文件名
NEW_FILENAME=`ls -lt $INSTALLPATH |grep redis-|head -n 1|awk '{print $9}'`;

if [ ! $NEW_FILENAME ];then
  echo "解压失败，获取不到解压后的文件名，退出脚本";
  exit
fi

#检查GCC是否安装
gcc -v

if [ $? -ne 0 ]; then
    echo "安装gcc环境";
     yum -y install gcc gcc-c++
fi

#进入redis解压后的文件目录
cd $NEW_FILENAME;

#对解压后的文件进行编译
make

#自定义安装目录
make PREFIX=$INSTALL_PATH install

#为了方便管理，将Redis文件中的conf配置文件和常用命令移动到统一文件中
#创建ect文件夹，将redis解压后目录下的 redis.conf 移动到 $INSTALL_PATH目录下的etc文件夹下
if [ ! -d $INSTALL_PATH/etc ];then
   mkdir $INSTALL_PATH/etc
fi
#cp $INSTALL_PATH/$NEW_FILENAME/redis.conf $INSTALL_PATH/etc

#清空$INSTALL_PATH/etc目录下的文件
cd $INSTALL_PATH/etc
rm -fr *

#自定义reids.conf配置文件
cat >>$INSTALL_PATH/etc/redis.conf<<EOF
#bind 127.0.0.1 -::1
protected-mode no
port $REDIS_PORT
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
#redis密码
requirepass $REDIS_PASSWORD
EOF

if [ $? -ne 0 ]; then
    echo "redis.conf配置文件配置失败，请排查原因，退出脚本";
    exit;
fi

#设置Redis开机启动
#cat >>/etc/rc.d/rc.local<<EOF
#$INSTALL_PATH/bin/redis-server $INSTALL_PATH/etc/redis.conf
#EOF

#启动redis
$INSTALL_PATH/bin/redis-server $INSTALL_PATH/etc/redis.conf

if [ $? -ne 0 ]; then
    echo "redis启动失败，请排查原因，退出脚本";
    exit;
else
    echo "redis 启动成功，请稍后手动登录云服务器,在安全组中放通$REDIS_PORT端口";
fi

